package com.example.library_common.album.ui

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.example.library_common.R
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.utils.GalleryDecoration
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

class AlbumOnlyPreviewFragment: SimpleListFragment<String>() {

    companion object{

        fun newInstance(): AlbumOnlyPreviewFragment{
            val args = Bundle()

            val fragment = AlbumOnlyPreviewFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var currentIndex=0

    fun setCurrentIndex(currentIndex:Int){
        this.currentIndex=currentIndex
    }

    fun addData(ls:List<String>){
        mData.clear()
        mData.addAll(ls)
    }

    private lateinit var title:TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isInitialRefresh = false
        isEnableLoadMore = false
        isAlwaysRefresh = false
    }



    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundColor(Color.BLACK)
        val snapHelper = PagerSnapHelper()
        mRecyclerView.onFlingListener = null
        snapHelper.attachToRecyclerView(mRecyclerView)
        mSwipeRefreshLayout.isEnabled = false
        initContentFrameView()
        mRecyclerView.scrollToPosition(currentIndex)
    }

    private fun initContentFrameView() {
        content_frame.removeAllViews()
        content_frame.visibility = View.VISIBLE
        LayoutInflater.from(_mActivity)
            .inflate(R.layout.album_only_preview_fragment_frame_view, content_frame)

        content_frame.findViewById<View>(R.id.backPage).setOnClickListener {
            onBackPressedSupport()
        }

        title=content_frame.findViewById(R.id.title)
    }

    override fun getItemLayout()= R.layout.album_only_preview_fragment

    override fun convertItem(helper: JssBaseViewHolder?, item: String?) {
        helper?.setImageNetUrl22(R.id.mPhotoView, item, R.drawable.img_erros_bg_rect)
    }

    @SuppressLint("SetTextI18n")
    override fun addItemDecoration(): RecyclerView.ItemDecoration {
        val galleryDecoration = GalleryDecoration()
        galleryDecoration.setIndexIndicator { index, itemCount ->

            title.text="${index+1}/${itemCount}"

        }
        return galleryDecoration
    }

    override fun getLayoutManager() =
        LinearLayoutManager(_mActivity, LinearLayoutManager.HORIZONTAL, false)

    override fun getListType():   Type = TypeBuilder.newInstance(CommonBean::class.java)
    .endSubType().build()

    override fun netRequest() {

    }

    override fun onRefresh() {
//        super.onRefresh()
    }
}